<?php

$action = '{approve} {reject}';
if($status == 'approved') {
    $action = '{reject}';
} else if($status == 'rejected') {
    $action = '{approve}';
}


$this->widget('zii.widgets.grid.CGridView', array(
    'id' => $status . '-request-grid',
    'dataProvider' => $dataProvider,
    'filter' => $model,
    'columns' => array(
        array(
            'header' => 'Request Id',
            'name' => 'id',
        ),
        'user_id',
        array(
            'name' => 'name',
        ),
        'date',
        'time_start',
        'time_end',
        'participant',
        array(
            'name' => 'room.name',
            'filter' => CHtml::activeTextField($model, 'room_name'),
            'sortable' => true,
        ),
        'message',
        array(
            'header' => 'Action',
            'class' => 'CButtonColumn',
            'template' => $action . '{view}',
            'buttons' => array(
                'approve' => array(
                    'label' => 'Approve',
                    'imageUrl' => $this->module->assetsUrl . '/images/approve.png',
                    'url' => '$this->grid->controller->createUrl("request/approve", array("id"=>$data->id))',
                    'click' => 'function(){$.fn.yiiGridView.update("'. $status . '-request-grid' . '", {
                                    type:"POST",
                                    url:$(this).attr("href"),
                                    success:function(data) {
                                            $.fn.yiiGridView.update("' . $status . '-request-grid' . '");
                                    },
                                    error:function() {
                                            alert("error");
                                            $.fn.yiiGridView.update("' . $status . '-request-grid' . '");
                                    }
                                });
                                return false;}'
                ),
                'reject' => array(
                    'label' => 'Reject',
                    'imageUrl' => $this->module->assetsUrl . '/images/reject.png',
                    'url' => '$this->grid->controller->createUrl("request/reject", array("id"=>$data->id))',
//                    'click' => 'function(){$.fn.yiiGridView.update("'. $status . '-request-grid' . '", {
//                                    type:"POST",
//                                    url:$(this).attr("href"),
//                                    success:function(data) {
//                                            $.fn.yiiGridView.update("' . $status . '-request-grid' . '");
//                                    },
//                                    error:function() {
//                                            alert("error");
//                                            $.fn.yiiGridView.update("' . $status . '-request-grid' . '");
//                                    }
//                                });
//                                return false;}',
                    'click' => 'function(){
                            openDialog($(this).attr("href"), "' . $status . '");
                            return false;
                        }'
                ),
            )
        ),
    )
    ));

$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
      'id'=>'reject-message-' . $status,
      // additional javascript options for the dialog plugin
      'options'=>array(
          'title'=>'Pesan',
          'autoOpen'=>false,
      ),
  ));

echo CHtml::textArea('', '', array('id' => 'message-text-area-' . $status));
        
$this->endWidget('zii.widgets.jui.CJuiDialog');
?>

<script type="text/javascript">
    function openDialog(url, status){
        $("#reject-message-" + status).dialog({
            title: 'Pesan',
            buttons : {
                "Click" : function(){
                    $.fn.yiiGridView.update(status + "-request-grid", {
                        type:"POST",
                        url:url,
                        data : "message=" + $("#message-text-area-" + status).val(),
                        success:function(data) {
                            $.fn.yiiGridView.update(status + "-request-grid");
                        },
                        error:function() {
                            $.fn.yiiGridView.update(status + "-request-grid");
                        }
                    });
                    $("#reject-message-" + status).dialog('close');
                    return true;
                }
            }
        });
        $("#reject-message-" + status).dialog("open");
    }
</script>
